var editRoleWName = null;
var isCurrDelRoleId = false;
//表格相关操作
var tablezEvents = {
	refresh:function(){//修改，新增，删除ajax成功回调函数
		roleDatatable.refresh();
	},
	onRenderItem:function(colName,text){
		switch(colName){
		case 'roleOperation':
			var html='<div class="tableRowsIcon">'+
						'<span title="删除"><a class="delABtn" href="javascript:;">'+
							'<i class="icon icon-trash"></i>'+
						'</a></span>'+
						'<span title="修改"><a class="editABtn" href="javascript:;">'+
							'<i class="icon icon-edit"></i>'+
						'</a></span>'+
					'</div>';
			return html;
		case 'state':
			var dis = text=="1"?"启用":"禁用";
			var color = text=="1"?"green":"red";
			return '<span style="color:'+color+'">'+dis+'</span>';
		case 'roleType':
			return $.getDropOptionText('#dropRoleType', text);
		}
	},
	onSingleClick:function(checksRecord,checkAll,currentRecord){
		
	},
	onDoubleClick:function(){
		var currRecord = roleDatatable.getCurrentRecord();
		$(".roleName").html(currRecord.data.roleName);
		$(".treeRole").val(currRecord.data.roleId);
		//加载资源信息
		queryItemResourceAjax.addParam("roleId",currRecord.data.roleId);
		queryItemResourceAjax.send();
	},
	onAfterReady:function(){
		//表格删除按钮
		$('.delABtn').click(function(e){
			e.stopPropagation();
			e.preventDefault();
			var num = $(this).parents('tr').index();
//				var record = roleDatatable.rows[num];
			var record = roleDatatable.records[num].data;
			MsgBox.confirm("确认删除角色【"+record.roleName+"】？","删除确认",function(){
				var id = record.roleId;
				//
				var treeRoleId = $(".treeRole").val();
				if(treeRoleId == id){
					isCurrDelRoleId = true;
				}else{
					isCurrDelRoleId = false;
				}
				//发送ajax删除单个记录
				var curr = roleDatatable.records[num].data;
				var arr = new Array();
				arr.push(curr);
				deleteRolesAjax.addParam("roles",JSON.stringify(arr));
				deleteRolesAjax.send();
			},null);
		});
		//表格修改按钮
		$(".editABtn").click(function(e){
			e.stopPropagation();
			e.preventDefault();
			var num = $(this).parents('tr').index();
			var curr = roleDatatable.records[num].data;
			$("#roleOldInfoFrm").autofill(curr);
			MsgBox.open("updateRole", "修改角色信息", ctx + "/sys/roleMgr/update/"+curr.roleId+"/Index", "600px");
			//MsgBox.trigger("", "", "#roleEditDialog");
		});
	}
}
//数相关操作
var treeEvents = {
		
}
//按钮点击事件
var buttonEvents = {
	onBatchDelete:function(){
		var selectRecords = roleDatatable.getSelectedRecords();
		if(selectRecords == null || selectRecords.length == 0){
			MsgBox.warn("请至少选择一条记录","消息提示",null);
		}else{
			MsgBox.confirm("确认删除所选【"+selectRecords.length+"】条角色信息？","删除确认",function(){
				isCurrDelRoleId = true;
				deleteRolesAjax.addParam("roles",JSON.stringify(listToRoles(selectRecords)));//待测试
				deleteRolesAjax.send();
			},null);
		}
	},
	onUpdateItem:function(){//修改角色基本信息
		var name = $("#roleOldInfoFrm input[name='roleName']").val();
		if(name == null || name == '' || name.length == 0){
			MsgBox.warn("角色名称不能为空","提示",function(){
				$("#roleOldInfoFrm input[name='roleName']").focus();
			});
		}else{
			var _obj = $("#roleOldInfoFrm").serializeObject();
			//
			var eRoleId = $("#roleOldInfoFrm input[name='roleId']").val();
			var treeRoleId = $(".treeRole").val();
			if(eRoleId == treeRoleId){
				editRoleWName = $("#roleOldInfoFrm input[name='roleName']").val();
			}else{
				editRoleWName = null;
			}
			$("#roleOldInfoFrm").submit();
			//发送ajax
			/*editRoleInfoAjax.setData(_obj);
			editRoleInfoAjax.send();*/
		}
	},
	onAddRole:function(){
		/*$('#createRoleFrm')[0].reset();
		MsgBox.trigger("", "", "#createRoleDialog");*/
		MsgBox.open("addRole", "新增角色信息", ctx + "/sys/roleMgr/add/Index", "600px", null, true);
	},
	onSaveResource:function(){//保存资源修改
		var roleId = $("#roleNameList").val();
		var selectTreeNodes = menusTreeqw.getCheckNodes(true);//所选择资源信息
		if(roleId == null || roleId == ""){
			MsgBox.info("请选择一条角色信息","提示",null);
		}else{
			var menuIds = new Array();
			for(var i=0;i<selectTreeNodes.length;i++){
				menuIds.push(selectTreeNodes[i].menuId);
			}
			editRoleResourceAjax.addParam("roleId",roleId);
			editRoleResourceAjax.addParam("menuIds",JSON.stringify(menuIds));
			editRoleResourceAjax.send();
		}
	},
	commitAddRoleInfo:function(){
		var name = $("#createRoleFrm input[name='roleName']").val();
		if(name == null || name == '' || name.length == 0){
			MsgBox.warn("角色名称不能为空","提示",function(){
				$("#createRoleFrm input[name='roleName']").focus();
			});
		}else{
			var a = 0;
			if(a = 0){
				$("#createRoleFrm").submit();
                a++;
			}
			/*var newRole = $("#createRoleFrm").serializeObject();
			//添加数据，发送ajax
			addRoleAjax.setData(newRole);
			addRoleAjax.send();*/
		}

    },
	closeDia:function(){
		var mWindow = MsgBox.getCurrentOpenDialogWindow();
        mWindow.close();
	}
}
var ajaxEvents = {
	onTableLoadSucc:function(data){//角色信息表 ajax完成后
        $("#roleNameList").empty();
        $("#roleNameList").append(
        	"<option>-请 选 择-</option>"
		)
		roles = data.info.data;
		$.each(roles,function(index,obj){
            $("#roleNameList").append(
				"<option value="+obj.roleId+">"+obj.roleName+"</option>"
            )
		})

	},
	resetA:function(){
		$('li>a').prop('href','javascript:void(0);');
		$('li>a').prop('target','');
		//展开所有节点
		menusTreeqw.expandAll();
	},
	onQueryResourceSucc:function(data){//查询单个角色资源信息填充资源树
		//设置选中的节点
		if(data.success == true){
			var menus = data.info;
			menusTreeqw.setCheckNode(menus,true,false,false);
		}
	},
	onDeleSucc:function(data){
		if(data.success == true){
			MsgBox.info("删除成功","提示",function(){
                tablezEvents.refresh();
			})
			//删除之后应清空菜单树
			if(isCurrDelRoleId){
				if(menusTreeqw != undefined && menusTreeqw != null){
					if(menusTreeqw.getCheckNodes(true).length != 0){
						menusTreeqw.checkAllNodes(false);
					}
				}
				$(".treeRole").val("");
				$("#showTree .roleName").html("");
				isCurrDelRoleId = false;
			}
		}
	},
	onEditInfoSucc:function(data){
		if(data.success == true){
			new $.zui.Messager('修改成功!', {
			    type: 'success',
			    time: 2000,
			    placement:'center'
			}).show();
			//修改之后应清空菜单树
			if(editRoleWName != null){
				$("#showTree .roleName").html(editRoleWName);
				editRoleWName = null;
			}
			setTimeout(function () {
				MsgBox.closeActiveDialog();
		    }, 2000);
		}
	},
	onEditResourceSucc:function(data){
		/*if(data.success == true){
			new $.zui.Messager('保存成功!', {
			    type: 'success',
			    time: 2000,
			    placement:'center'
			}).show();
		}*/
		MsgBox.info("设置成功","提示",null)
	},
	onCreateSucc:function(){
		MsgBox.info("新增成功","提示",null);
        MsgBox.closeActiveDialog();
	}
}
function RoleSel(data){
	this.roleId = data.data.roleId;
	this.roleName = data.data.roleName;
	this.remark =  data.data.remark;
	this.state = data.data.state;
	this.roleType =  data.data.roleType;
}
function listToRoles(seleArr){
	var arr = new Array();
	for(var i=0;i<seleArr.length;i++){
		var newRole = new RoleSel(seleArr[i]);
		arr.push(newRole);
	}
	return arr;
}
function searchName() {
	roleManaPagination.resetPagination();
	queryRoleInfoListAjax.send();
}
$(function(){
	var value = $("#state").attr("data-value");
	$("#state").val(value);
})

function getMenus(){
	var currRoleId = $("#roleNameList").val();
	if(currRoleId == null || currRoleId==''){
		return false;
	}
	queryItemResourceAjax.addParam("roleId",currRoleId)
    queryItemResourceAjax.send();
}